import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeStay from '@/views/front_desk/Home/HomeStay'
import HomeStayMenu from '@/views/backstage_supporter/Menu/HomeStayMenu/HomeStayMenu'
import Finance from '@/views/backstage_supporter/Menu/MenuList/FinanceManagement'
import HousingResource from '@/views/backstage_supporter/Menu/MenuList/HousingResource'
import Order from '@/views/backstage_supporter/Menu/MenuList/OrderManagement'
import User from '@/views/backstage_supporter/Menu/MenuList/UserManagement'
import Landlord from '@/views/backstage_supporter/Menu/MenuList/LandlordManagement'
import HouseInformation from '@/views/front_desk/Information/HouseInformation'
import UserLogin from '@/views/front_desk/Login/UserLogin'
import Anroll from '@/views/front_desk/Login/Anroll'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/homestay'
  },
  // {
  //   path: '/',
  //   redirect: '/menu'
  // },
  {
    name: 'homestay',
    path: '/homestay',
    component: HomeStay
  },
  {
    name: 'houseInformation',
    path: '/houseInformation/:id',
    component: HouseInformation
  },
  {
    path: '/userLogin',
    component: UserLogin
  },
  {
    path: '/anroll',
    component: Anroll
  },
  {
    path: '/menu',
    component: HomeStayMenu,
    children: [
      {
        path: 'finance',
        component: Finance
      },
      {
        path: 'housing',
        component: HousingResource
      },
      {
        path: 'order',
        component: Order
      },
      {
        path: 'user',
        component: User
      },
      {
        path: 'landlord',
        component: Landlord
      }
    ]
  }
]

const router = new VueRouter({
  routes

})

router.beforeEach((to, from, next) => {
  if (to.path !== '/userLogin' && to.path !== '/homestay' && to.path !== '/anroll') {
    // 要访问后台主页 需要判断是否有 tag
    const token = localStorage.getItem('token')
    // 如果tag值存在 则放行
    if (token) {
      next()
      // 没有token 则直接强制跳转到登录页
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
